﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using Register.BLL;
using Register.Model;

namespace RegisterWeb.Manager.SystemUserManager.ashx
{
    /// <summary>
    /// RoleModelService 的摘要说明
    /// </summary>
    public class RoleModelService : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string action = context.Request["action"];
            action = action;
            if (action == "get")
            {
                //int intPageSize = int.Parse(context.Request["rows"].ToString());
                //int intCurrentPage = int.Parse(context.Request["page"].ToString());
                string id = context.Request["Roleid"].ToString();
                var rolemodllist = RoleModelInfoBLL.GetList(id);
                var list = ModuleInfoBLL.GetList("1=1");
                foreach (var row in list)
                {
                    row.ModuleInfo_int= 0;
                    var toplist=ModuleTopInfoBLL.GetModuleTopInfosByID(row.ModuleTop_ID);
                    row.ModuleTopInfo_Name = toplist[0].ModuleTop_Name;
                }
                for (int i = 0; i < list.Count; i++)
                {
                    for (int j = 0; j < rolemodllist.Count; j++)
                    {
                        if (list[i].ModuleInfo_ID == rolemodllist[j].ModuleInfo_ID)
                        {
                            list[i].ModuleInfo_int = 1;
                        }
                    }
                }
                JavaScriptSerializer jss = new JavaScriptSerializer();
                context.Response.Write(jss.Serialize(list));
            }
                  else if (action == "add") 
               {
                  RoleModel rolemodel=new RoleModel();
                  rolemodel.ModuleInfo_ID=context.Request["ModuleInfo_ID"].ToString();
                  rolemodel.Role_ID= context.Request["Role_ID"].ToString();
                  rolemodel.RoleModel_Describe = "1";
                 rolemodel.RoleModel_AddTime = DateTime.Now;
                 rolemodel.RoleModel_Recoder = "1";
                 rolemodel.RoleModel_LastAmendTime = DateTime.Now;
                 rolemodel.RoleModel_LastAmendPerson= "1";
                 rolemodel.RoleModel_State="1";
             if( RoleModelInfoBLL.Add(rolemodel)>0)
                 {
                     context.Response.Write("ok");
                 }
                 else
                 {
                     context.Response.Write("no");
                 }
               }
               else if (action == "delete")
               {
               string       ModuleInfo_ID=context.Request["ModuleInfo_ID"].ToString();
              string Role_ID= context.Request["Role_ID"].ToString();
              if (RoleModelInfoBLL.Delete(Role_ID, ModuleInfo_ID))
                {
                    context.Response.Write("ok");
                }
                else
                {
                    context.Response.Write("no");
                }
            }
        }

        

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}